/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.adapters.impl;

import cz.muni.fi.pa165.bookingmanager.adapters.AdministratorAdapter;
import cz.muni.fi.pa165.bookingmanager.dto.AdministratorDTO;
import cz.muni.fi.pa165.bookingmanager.entities.Administrator;
import cz.muni.fi.pa165.bookingmanager.exceptions.IllegalOrphanException;
import cz.muni.fi.pa165.bookingmanager.exceptions.NonexistentEntityException;
import cz.muni.fi.pa165.bookingmanager.exceptions.PreexistingEntityException;
import cz.muni.fi.pa165.bookingmanager.services.AdministratorService;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Jiří Holuša
 */
@Stateless
public class AdministratorAdapterImpl implements AdministratorAdapter {

    @EJB
    private AdministratorService administratorService;

    @Override
    public void createAdministrator(AdministratorDTO administrator) throws IllegalOrphanException, PreexistingEntityException {
        Administrator administratorEntity = AdaptersHelper.administratorDTOToEntity(administrator);
        administratorService.createAdministrator(administratorEntity);
        administrator.setId(administratorEntity.getId());
    }

    @Override
    public void updateAdministrator(AdministratorDTO administrator) throws NonexistentEntityException, IllegalOrphanException, PreexistingEntityException, Exception {
        Administrator administratorEntity = AdaptersHelper.administratorDTOToEntity(administrator);
        administratorService.updateAdministrator(administratorEntity);
    }

    @Override
    public void deleteAdministrator(AdministratorDTO administrator) throws NonexistentEntityException, IllegalOrphanException {
        Administrator administratorEntity = AdaptersHelper.administratorDTOToEntity(administrator);
        administratorService.deleteAdministrator(administratorEntity);
    }

    @Override
    public AdministratorDTO getAdministratorById(Long administratorId) {
        Administrator administratorEntity = administratorService.getAdministratorById(administratorId);
        AdministratorDTO administrator = AdaptersHelper.administratorEntityToDTO(administratorEntity);
        return administrator;
    }
    
    @Override
    public AdministratorDTO getAdministratorByUserName(String username) {
        Administrator administratorEntity = administratorService.getAdministratorByUserName(username);
        AdministratorDTO administrator = AdaptersHelper.administratorEntityToDTO(administratorEntity);
        return administrator;
    }

    @Override
    public List<AdministratorDTO> findAllAdministrators() {
        List<Administrator> administratorEntity = administratorService.findAllAdministrators();
        List<AdministratorDTO> administrators = new ArrayList<AdministratorDTO>();
        for (Administrator cust : administratorEntity) {
            administrators.add(AdaptersHelper.administratorEntityToDTO(cust));
        }
        return administrators;
    }

    public void setAdministratorService(AdministratorService administratorService) {
        this.administratorService = administratorService;
    }
}
